package com.lu.integrate.message.center.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.lu.integrate.message.center.model.request.MessageCenterQueueMessageConfirmSubmitRequest;
import com.lu.integrate.message.center.model.request.MessageCenterQueueProduceRequest;
import com.lu.integrate.message.center.service.MessageQueueAbstract;
import com.lu.integrate.message.center.service.MessageQueueService;
import com.lu.stack.common.model.response.R;
import com.lu.integrate.message.center.model.request.MessageCenterQueueConsumerRequest;
import com.lu.integrate.message.center.model.request.MessageCenterQueueRetryConsumerRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author zhanglu
 * @project lu-cloud-stack
 * @description 消息中心
 * @date 2023/2/13 18:03
 */
@Slf4j
@RequestMapping("/message-center")
@RestController
public class MessageCenterController {

    //队列
    public static final String QUEUE = "/queue";

    @Resource
    private MessageQueueAbstract messageQueueAbstract;

    /***********************队列***********************/

    /**
     * 消息发送生产者保存
     * @param queueHandler
     * @return
     */
    @PostMapping(QUEUE + "/producer")
    public R queueProducer(@RequestBody @Validated MessageCenterQueueProduceRequest queueHandler) throws ClassNotFoundException {
        MessageQueueService messageQueueService = (MessageQueueService) SpringUtil.getBean(Class.forName(queueHandler.getType().getBeanClass()));
        return messageQueueService.produce(queueHandler);
    }

    /**
     * 重试消费
     * @param request
     * @return
     */
    @PostMapping(QUEUE + "/retry-consume")
    public R retryConsume(@RequestBody @Validated MessageCenterQueueRetryConsumerRequest request) throws ClassNotFoundException {
        MessageQueueService messageQueueService = (MessageQueueService) SpringUtil.getBean(Class.forName(request.getType().getBeanClass()));
        return messageQueueService.retryConsume(request);
    }

    /**
     * 消息消费
     * @param request
     * @return
     */
    @PostMapping(QUEUE + "/consume")
    public R consume(@RequestBody @Validated MessageCenterQueueConsumerRequest request) {
        return messageQueueAbstract.consume(request);
    }

    /**
     * 消息消费完毕，确认提交
     * @param request
     * @return
     */
    @PostMapping(QUEUE + "/confirm-submit")
    public R confirmSubmit(@RequestBody @Validated MessageCenterQueueMessageConfirmSubmitRequest request) throws ClassNotFoundException {
        MessageQueueService messageQueueService = (MessageQueueService) SpringUtil.getBean(Class.forName(request.getType().getBeanClass()));
        return messageQueueService.confirmSubmit(request);
    }

}
